package com.tianyu.inspection.common;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description
 * @Date 2020.08.20 - 14:50
 **********************************/

@Slf4j
public class QueryModel {

    /**
     * 获取分页数据
     *
     * @param queryMo
     * @param mapper
     * @return
     */
    public static <T> IPage<T> getModelPage(JSONObject queryMo, BaseMapper<T> mapper) {
        return getModelPage(new QueryWrapper<T>(), new Page<>(), mapper);
    }

    /**
     * 获取分页数据
     *
     * @param wrapper
     * @param mapper
     * @return
     */
    public static <T> IPage<T> getModelPage(QueryWrapper<T> wrapper, Page<T> page, BaseMapper<T> mapper) {
        return mapper.selectPage(page, wrapper);
    }


    public static <T> IPage<T> getModelPage(QueryWrapper<T> wrapper, BaseMapper<T> mapper, JSONObject queryMo) {
        Page<T> page;
        if (Objects.nonNull(queryMo)) {
            final Integer pageNum = queryMo.getInteger("pageNum");
            final Integer pageSize = queryMo.getInteger("pageSize");
            page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        } else {
            page = new Page<>(1, -1);
        }
        return mapper.selectPage(page, wrapper);
    }

    /**
     * 获取分页数据
     *
     * @param queryMo
     * @param mapper
     * @return
     */
    public static <T> IPage<T> getModelPage(Class<T> type,
                                            QueryWrapper<T> wrapper,
                                            BaseMapper<T> mapper,
                                            JSONObject queryMo, String... eqExcludeFields) {
        Page<T> page;
        if (Objects.nonNull(queryMo)) {
            final Integer pageNum = queryMo.getInteger("pageNum");
            final Integer pageSize = queryMo.getInteger("pageSize");
            page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
            wrapper = getModelWrapper(type, wrapper, queryMo, eqExcludeFields);
        } else {
            page = new Page<>(1, -1);
        }
        return mapper.selectPage(page, wrapper);
    }

    /**
     * 获取分页数据
     *
     * @param queryMo
     * @return
     */
    public static <T> QueryWrapper<T> getModelWrapper(Class<T> type, QueryWrapper<T> wrapper, JSONObject queryMo, String... eqExcludeFields) {
        if (wrapper == null) {
            wrapper = new QueryWrapper<>();
        }
        final String ascParams = queryMo.getString("ascParams");
        final String descParams = queryMo.getString("descParams");
        final JSONObject eqParams = queryMo.getJSONObject("eqParams");
        final JSONObject inParams = queryMo.getJSONObject("inParams");
        final JSONObject likeParams = queryMo.getJSONObject("likeParams");
        if (Objects.nonNull(eqParams) && !eqParams.isEmpty()) {
            log.info("eqParams: " + eqParams);
            final JSONObject eqMap = ObjectModel.getEqOrLikeMap(type, eqParams, eqExcludeFields);
            if (!eqMap.isEmpty()) {
                wrapper.allEq(eqMap);
            }
        }
        if (Objects.nonNull(likeParams) && !likeParams.isEmpty()) {
            log.info("likeParams: " + likeParams);
            JSONObject likeMap = ObjectModel.getEqOrLikeMap(type, likeParams, eqExcludeFields);
            if (!likeMap.isEmpty()) {
                likeMap.forEach(wrapper::like);
            }
        }
        if (Objects.nonNull(inParams) && !inParams.isEmpty()) {
            log.info("inParams: " + inParams);
            Map<String, List<String>> inMap = ObjectModel.getInMap(type, inParams, eqExcludeFields);
            if (!inMap.isEmpty()) {
                inMap.forEach(wrapper::in);
            }
        }
        if (Objects.nonNull(ascParams) && !Objects.equals(ascParams, "")) {
            log.info("ascParams: " + ascParams);
            wrapper.orderByAsc(ObjectModel.getSortParams(type, queryMo.getString("ascParams")));
        }
        if (Objects.nonNull(descParams) && !Objects.equals(descParams, "")) {
            log.info("descParams: " + descParams);
            wrapper.orderByDesc(ObjectModel.getSortParams(type, queryMo.getString("descParams")));
        }
        return wrapper;
    }

    /**
     * 获取分页数据
     *
     * @param queryMo
     * @return
     */
    public static <T> IPage<T> getModelPage(JSONObject queryMo) {
        Page<T> page;
        final Integer pageNum = queryMo.getInteger("pageNum");
        final Integer pageSize = queryMo.getInteger("pageSize");
        if (pageNum != null && pageSize != null) {
            page = new Page<>(pageNum, pageSize);
        } else {
            page = new Page<>();
        }
        return page;
    }
}
